package com.ihealthway.all.groupbuy.services;

import com.ihealthway.all.groupbuy.vo.SysAdmin;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;

import java.util.*;

/**
 * System admin service
 *
 * @author 凡梦星尘(elkan1788@gmail.com)
 */
@IocBean(fields = {"dao"})
public class SysAdminService extends BaseService<SysAdmin> {

    private static final Log logger = Logs.get();

    public SysAdmin checkLogin(String loginAct) {
        Cnd cnd = Cnd.where("account", "=", loginAct).or("email", "=", loginAct);
        SysAdmin sysAdmin = null;
        try {
            List<SysAdmin> sysAdmins = query(cnd);
            if (sysAdmins!=null && !sysAdmins.isEmpty()) {
                sysAdmin = sysAdmins.get(0);
            }
        } catch (Exception e) {
            logger.errorf("account:%s, email:%s", loginAct, loginAct);
            logger.error("System admin login failed", e);
        }

        return sysAdmin;
    }

    public boolean update(SysAdmin sysAdmin) {

        boolean updated = false;

        sysAdmin.setUpdateTime(new Date());
        try {
            int effectRows = dao().updateIgnoreNull(sysAdmin);
            logger.infof("Update system admin effect rows: %s", effectRows);
            updated =  (effectRows != 0);
        } catch (Exception e) {
            logger.error("Update system admin failed.", e);
        }

        return updated;
    }

}
